package org.jsbd.boss.service.impl;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.dao.ICameraDao;
import org.jsbd.boss.domian.channel.CameraInfo;
import org.jsbd.boss.service.ICameraService;
import org.jsbd.boss.util.CacheKey;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @desc
 * @Date 2018/8/20 17:02
 * @Author xupengcheng [xupengcheng@zhexinit.com]
 */
@Service
public class CameraServiceImpl implements ICameraService {

    @Resource
    private RedisClient redisClient;

    @Resource
    private ICameraDao cameraDao;


    @Override
    public CameraInfo selectCamera(String hsman, String hstype) {
        String key = CacheKey.CAMERA_EXIST_INFO_KEY;
        String field = hsman + ":" + hstype;
        CameraInfo cameraInfo = (CameraInfo) redisClient.getHash(key, field);
        if (cameraInfo != null) {
            return cameraInfo;
        }

        cameraInfo = cameraDao.selectCamera(hsman, hstype);
        if (cameraInfo != null) {
            redisClient.putHash(key, field, cameraInfo);
        }
        return cameraInfo;
    }

    @Override
    public CameraInfo selectRandomCamera(String hsman, String hstype) {

        String key = CacheKey.CAMERA_INFO_KEY;
        String field = hsman + ":" + hstype;
        CameraInfo cameraInfo = (CameraInfo) redisClient.getHash(key, field);
        if (cameraInfo != null) {
            return cameraInfo;
        }

        cameraInfo = cameraDao.selectRandomCamera(hsman, hstype);
        if (cameraInfo != null) {
            redisClient.putHash(key, field, cameraInfo);
        }
        return cameraInfo;
    }
}
